﻿using System;
using System.Collections.Generic;
using MAF.Common;

namespace MAF.UserSecurity.Repository
{
    public class UserContextRepo
    {
        public virtual void Add(UserContext user)
        {
            _UserContexts.Add(user);
        }

        public virtual int Remove(Guid guid)
        {
            Query<UserContext> q = new Query<UserContext>();
            q.AddCriterion("Token", Op.Equals, guid);
            return RemoveByQuery(q);
        }

        /// <summary>
        /// 删除符合过滤条件的记录
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public int RemoveByQuery(Query<UserContext> q)
        {
            return _UserContexts.RemoveAll(delegate(UserContext entity)
            {
                foreach(Criterion<UserContext> c in q)
                {
                    return c.IsMath(entity);
                }
                return false;
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public List<UserContext> GetByQuery(Query<UserContext> q)
        {
            return _UserContexts.FindAll(delegate(UserContext entity)
            {
                foreach (Criterion<UserContext> c in q)
                {
                    return c.IsMath(entity);
                }
                return false;
            });
        }

        /// <summary>
        /// 用户上下文内存存储集合
        /// </summary>
        private static List<UserContext> _UserContexts = new List<UserContext>();
    }
}